package a03.dao;

import a03.pojo.Major;

import java.sql.*;
import java.util.ArrayList;

public class MajorDao {

    public ArrayList<Major> findAllMajors(){
        ArrayList<Major> allMajors=new ArrayList<>();
        try {
            //1.使用JDBC查询所有专业信息
            //1.1 加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");
            //1.2 建立与数据库的连接
            String url = "jdbc:mysql://localhost:3306/javaeelab";
            Connection conn = DriverManager.getConnection(url, "root", "root");
            //1.3 创建命令对象
            Statement st = conn.createStatement();
            //1.4 执行SQL语句
            String sql = "SELECT * FROM tb_major";
            ResultSet rs = st.executeQuery(sql);
            //1.5 将所查询到数据封装到集合中

            while (rs.next()){
                Major major=new Major();
                major.setId(rs.getString("id"));
                major.setMajorName(rs.getString("major_name"));
                allMajors.add(major);
            }
            //1.6 释放资源
            rs.close();
            st.close();
            conn.close();

        }catch (Exception e){
            e.printStackTrace();
        }
        return allMajors;
    }
}
